/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rpg.tools;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Emmanouil Samatas
 */
public class FileManager {
    public static volatile FileManager instance = null;
    Scanner scanner ;
    
    private FileManager(){};
    
    public static FileManager getInstance(){
        if (instance == null){
            synchronized(FileManager .class){
                if (instance == null){
                    instance = new FileManager();
            }
            }
        }
            return instance;
      
    }
    
    public List<String> loadMap(int level){
        List<String> map = new ArrayList();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        String filePath = new File(".").getAbsolutePath()+"//maps//";
        sb.append(String.valueOf(level));
        sb.append(".txt");
        String filename = sb.toString();
  
        try{
        FileReader fileReader = new FileReader(filePath + filename);

        br = new BufferedReader(fileReader);
        String currentLine;
        int count = 0;
        while ((currentLine = br.readLine()) != null) {
               map.add(currentLine);
               count++;
			}
        }catch(Exception e){
            System.out.println("Cannot load map");
        }finally{
            try{
                if(br != null){
                    br.close();
                }}catch(IOException e){
                    System.out.println("Error closing the file");
                    
                }
            }
        
        
       //openMapFile(filename);
       // map = readMapFile();
       // closeMapFile();
        return map;
        
    }
    
   
    //test
    public static void main(String[] args){
        FileManager fm = FileManager.getInstance();
        List<String> map = fm.loadMap(1);
        System.out.println(map);
    }
    
}
